package com.atguigu.crowd.handler;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.aliyuncs.ecs.model.v20140526.DescribeBandwidthPackagesResponse.BandwidthPackage.PublicIpAddresse;
import com.atguigu.crowd.api.MySQLRemoteService;
import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.vo.AddressVO;
import com.atguigu.crowd.entity.vo.MemberLoginVO;
import com.atguigu.crowd.entity.vo.OrderProjectVO;
import com.atguigu.crowd.util.ResultEntity;

@Controller
public class OrderHandler {
	
	@Autowired
	private MySQLRemoteService mySQLRemoteService;
	
	
	
	//显示回报信息
	@RequestMapping("/confirm/return/info/{returnId}")
	public String showReturnConfirmInfo(
			@PathVariable("returnId") Integer returnId,
			HttpSession session) {
		ResultEntity<OrderProjectVO> resultEntity = mySQLRemoteService.getOrderProjectVORemote(returnId);
		if (ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
			OrderProjectVO data = resultEntity.getData();
			//存入session中
			session.setAttribute("orderProjectVO", data);
		}
		return "confirm_return";
	}

	//显示确认订单信息
	@RequestMapping("/confirm/order/{returnCount}")
	public String showOrderConfirm(
			@PathVariable("returnCount") Integer returnCount,
			HttpSession session) {
		//从session中获取OrderProject对象
		OrderProjectVO orderProjectVO = (OrderProjectVO)session.getAttribute("orderProjectVO");
		//设置输入的数量
		orderProjectVO.setReturnCount(returnCount);
		//获取AddressVO
		MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
		Integer memberId = memberLoginVO.getId();
		ResultEntity<List<AddressVO>> resultEntity = mySQLRemoteService.getAddressVORemote(memberId);
		if (ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
			List<AddressVO> data = resultEntity.getData();
			session.setAttribute("addressVOList", data);
		} 
		return "confirm_order";
	}
	//添加收货地址
	@RequestMapping("/save/address")
	public String saveAddress(AddressVO addressVO,HttpSession session) {
		ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressVORemote(addressVO);
		//从session中获取returnCount
		OrderProjectVO orderProjectVO = (OrderProjectVO)session.getAttribute("orderProjectVO");
		Integer returnCount = orderProjectVO.getReturnCount();
		//重定向到页面显示地址信息，需要returnCount
		return "redirect:http://localhost/order/confirm/order/"+returnCount;
	}
	
}
